<style>
    .layui-input-inline{margin-top:10px;}
</style>
<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>代付管理</cite></a>
        <a><cite>代付订单</cite></a>
    </span>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">手动代付列表</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-row">
                    <div class="layui-form" style="float:right; margin-bottom: 10px;">
                        <div id="timeDiv" class="layui-form-item" style="margin:0;">
                            <div class="layui-input-inline">
                                <input type="text" name="mchId" id="searchMchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="accountName" id="searchAccountName" placeholder="账户名" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="agentpayOrderId" id="searchAgentpayOrderId" placeholder="代付单号" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="mchOrderNo" id="searchMchOrderNo" placeholder="商户单号" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="transOrderId" id="searchTransOrderId" placeholder="转账单号" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <select name="agentpayChannel" id="searchAgentpayChannel" lay-search="">
                                    <option value="">代付渠道</option>
                                    <option value="1">商户后台</option>
                                    <option value="2">API接口</option>
                                </select>
                            </div>
                            <a id="search" style="margin-top: 10px" class="layui-btn" data-type="reload"><i class="layui-icon layui-icon-search"></i>搜索</a>
                        </div>
                    </div>
                    <div class="layui-form-item"></div>
                </div>
                <div class="layui-row">
                    <div class="layuiAdmin-btns" style="margin-bottom: 10px;">
                        <a class="layui-btn layui-btn-sm layui-hide" id="batchToSupplierBtn"><i class="layui-icon layui-icon-release"></i>批量转X</a>
                    </div>
                </div>
                <table id="jsList"  lay-filter="jsList"></table>
            </div>
        </div>
    </div>
</div>
<script>
     //标题内容模板
     var tplEdit = function(d){

         if(d.displayHandle == 1) {
             if(d.status != 4){ //只支持状态为 待审核的订单
                 return "";
             }

             if(d.supplierExecStatus == 0){ //无需MS处理
                 return "<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='toProc'><i class='layui-icon layui-icon-release'></i>处理</a>";
             }else if(d.supplierExecStatus == 1){ //MS处理中
                 return "";
             }else if(d.supplierExecStatus == 4){ //接口自动处理
                 return "";
             }else if(d.supplierExecStatus == 5){ //审核完成
                 return "";
             }else{ //MS完成 或者 MS拒绝处理时需要 进行下一步动作
                 return "<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='auditSupplierExec'><i class='layui-icon layui-icon-edit'></i>审核MS处理结果</a>";
             }
         } else {
             return '<button type="button" class="layui-btn layui-btn-xs layui-btn-disabled">处理中</button>';
         }

     };

     var tplStatus = function(d){
         if(d.status == 0) {
             return '<span class="layui-badge layui-bg-blue">待处理</span>';
         }else if(d.status == 1) {
             return '<span class="layui-badge layui-bg-orange">处理中</span>';
         }else if(d.status == 2) {
             return '<span class="layui-badge layui-bg-green">成功</span>';
         }else if(d.status == 3) {
             return '<span class="layui-badge">失败</span>';
         }else if(d.status == 4) {
             return '<span class="layui-badge layui-bg-orange">待审核</span>';
         }
     };

     var tplSupplierExecStatus = function(d){
         if(d.supplierExecStatus == 0) {
             return '<span class="layui-badge layui-bg-blue">未分配MS</span>';
         }else if(d.supplierExecStatus == 1) {
             return '<span class="layui-badge layui-bg-orange">MS处理中</span>';
         }else if(d.supplierExecStatus == 2) {
             return '<span class="layui-badge layui-bg-green">处理完成待审核</span>';
         }else if(d.supplierExecStatus == 3) {
             return '<span class="layui-badge">MS拒绝处理</span>';
         }else if(d.supplierExecStatus == 4) {
             return '<span class="layui-badge">自动接口处理</span>';
         }else if(d.supplierExecStatus == 5) {
             return '<span class="layui-badge">审核完成</span>';
         }
     };

     var tplAgentpayChannel = function(d){
         if(d.agentpayChannel == 1) {
             return '商户后台';
         }else if(d.agentpayChannel == 2) {
             return 'API接口';
         }
     };

     layui.use(['table','util','admin', 'timePick'], function(){
        var table = layui.table
        ,$ = layui.$
        ,admin = layui.admin
        ,timePick = layui.timePick
        ,element = layui.element
        ,view = layui.view
        ,form = layui.form;

         timePick.init();  //第一个参数表示开始时间   第二个表示结束时间   0,0今天   -1,-1昨天   -7,0近一周   以此类推
        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

         // 初始化统计结果
         var createTimeStart = $('#createTimeStart').val();
         var createTimeEnd = $('#createTimeEnd').val();
         var agentpayOrderId = $('#searchAgentpayOrderId').val();
         var transOrderId = $('#searchTransOrderId').val();
         var accountName = $('#searchAccountName').val().trim();;
         var mchId = $('#searchMchId').val();
         var agentpayChannel = $("#searchAgentpayChannel").val();
         var mchOrderNo = $("#searchMchOrderNo").val();

        table.render({
            elem: '#jsList'
            ,id: "tableReload"
            ,url: layui.setter.baseUrl + '/agentpay/list'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token,
                createTimeStart: createTimeStart,
                createTimeEnd: createTimeEnd,
                status: 4
            }
            ,cols: [[
                {type:'checkbox'}
                ,{field: 'mchId', title: '商户ID'}
                ,{field: 'mchName', title: '商户名称'}
                ,{field: 'agentpayOrderId', title: '代付单号'}
                ,{field: 'mchOrderNo', title: '商户单号'}
                ,{field: 'accountName',title: '账户名'}
                ,{field: 'accountNo',title: '账号'}
                ,{field: 'amount', title: '代付金额(元)',templet: '<div>{{d.amount/100}}</div>' }
                ,{field: 'fee', title: '手续费(元)',templet: '<div>{{d.fee/100}}</div>' }
                ,{field: 'fee', title: '扣减账户金额(元)',templet: '<div>{{d.subAmount/100}}</div>' }
                ,{field: 'status',title: '状态',templet: tplStatus}
                ,{field: 'status',title: '转交MS状态',templet: tplSupplierExecStatus, width: 130}
                ,{field: 'supplierExecId',title: '转交MSID', width: 100}
                ,{field: 'agentpayChannel',title: '代付渠道',templet: tplAgentpayChannel}
                ,{field: 'createTime',  title: '时间', width: 200, templet: '<div>{{ layui.util.toDateString(d.createTime) }}</div>' }
                ,{field: 'edite', title: '操作', width: 180, fixed: 'right', templet: tplEdit }
            ]]
            ,page: true
            ,skin: 'line'
        });

         //监听工具条
         table.on('tool(jsList)', function(obj){
             var data = obj.data;
             if(obj.event === 'toProc'){
                 // 请求后台，返回是否可以处理该订单
                 admin.req({
                     type: "POST",
                     url: layui.setter.baseUrl + '/agentpay/snatch',
                     data: {
                         agentpayOrderId : data.agentpayOrderId
                     },
                     success: function(res){
                         var code = res.code;
                         var msg = res.msg;
                         if(code == 0) {
                             if(msg == 'ok') {
                                 view.xxpayPopup("详情", "agentpay/agentpay_audit_record/audit", {agentpayOrderId: data.agentpayOrderId}, {btn: false});
                             }else {
                                 table.reload('tableReload');//刷新表格
                                 layer.alert(msg, {title: "提示"}, function(index){
                                     layer.close(index);
                                 });
                             }
                         }else {
                             table.reload('tableReload');//刷新表格
                         }
                     }
                 });

             }
         });

         // 搜索
         var $ = layui.$, active = {
             reload: function(){
                 var createTimeStart = $('#createTimeStart').val();
                 var createTimeEnd = $('#createTimeEnd').val();
                 var agentpayOrderId = $('#searchAgentpayOrderId').val();
                 var transOrderId = $('#searchTransOrderId').val();
                 var accountName = $('#searchAccountName').val().trim();;
                 var mchId = $('#searchMchId').val();
                 var agentpayChannel = $("#searchAgentpayChannel").val();
                 var mchOrderNo = $("#searchMchOrderNo").val();
                 //执行重载
                 table.reload('tableReload', {
                     page: {
                         curr: 1 //重新从第 1 页开始
                     }
                     ,where: {
                         accountName: accountName,
                         agentpayOrderId: agentpayOrderId,
                         transOrderId: transOrderId,
                         mchId: mchId,
                         createTimeStart : createTimeStart,
                         createTimeEnd : createTimeEnd,
                         agentpayChannel: agentpayChannel,
                         mchOrderNo: mchOrderNo,
                         status : 4
                     }
                 });
             }
         };
         $('#search').on('click', function() {
             var type = $(this).data('type');
             active[type] ? active[type].call(this) : '';
         });

         //点击【批量转交MS】
         $("#batchToSupplierBtn").click(function(){

             // 获取表格选中行的数据
             var checkDatas = table.checkStatus('tableReload').data;
             if(checkDatas.length <= 0){
                 layer.alert('请选择数据！');
                 return false;
             }

             let batchAgentpayOrderIdList = [];
             for(let i = 0; i < checkDatas.length; i++){
                 let d = checkDatas[i];

                 if(d.displayHandle == 1) {
                     if(d.status != 4){ //只支持状态为 待审核的订单
                         layer.alert('请选择状态为待审核并且支持处理的订单！');
                         return false;
                     }

                     if(d.supplierExecStatus != 0){ //无需MS处理
                         layer.alert('请选择状态为待审核并且支持处理的订单！');
                         return false;
                     }
                 } else {
                     layer.alert('请选择状态为待审核并且支持处理的订单！');
                     return false;
                 }
                 batchAgentpayOrderIdList.push(d.agentpayOrderId);
             }

             view.xxpayPopup("批量转交MS处理", "agentpay/agentpay_audit_record/batch_audit_2supplier", {agentpayOrderIdList: batchAgentpayOrderIdList}, {btn: false});

         });

         //点击【批量审核MS处理结果】
         $("#batchSupplierExecAuditBtn").click(function(){






         });

         //查询支持的打款方式
         admin.req({
             type: 'get',
             url: layui.setter.baseUrl + '/agentpay/getAgentpayChoosePayTypeConfig',
             error: function(err){layer.alert(err);},
             success: function(res){
                 if(res.code == 0){
                     var agentpayChoosePayTypeArray = JSON.parse(res.data.agentpayChoosePayType);
                     $(agentpayChoosePayTypeArray).each(function(){
                        if(this == 99 && res.data.agentpaySnatchFlag != 1){ //允许 转交MS处理 && 未开启 抢单模式
                            $("#batchToSupplierBtn").removeClass("layui-hide");
                        }
                     });
                 }
             }
         });

         // 渲染表单
         form.render();

     });


</script>